package com.wx.tnsadmin.controller;

import com.github.pagehelper.PageInfo;
import com.wx.common.api.CommonResult;
import com.wx.common.api.ResultCode;
import com.wx.tnsadmin.pojo.SysLog;
import com.wx.tnsadmin.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:LLH
 * @Date:2020/8/19
 */
@RequestMapping(value = "/log")
@RestController
@Api(tags = "后台日志管理")
public class LogController {

    @Autowired
    private LogService logService;


    @ApiOperation(value = "后台日志分页查询", notes = "后台日志分页查询")
    @RequestMapping(value = "/findByLogPage", method = RequestMethod.GET)
    public CommonResult findByLogPage ( SysLog log ) {
        PageInfo byLogPage = logService.findByLogPage ( log );
        try {
            if (byLogPage == null) {
                return CommonResult.failed ( "分页查询日志失败!" );
            } else {
                return CommonResult.success ( byLogPage,"分页查询日志成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }


    @ApiOperation(value = "后台日志关键字查询", notes = "后台日志关键字查询")
    @RequestMapping(value = "/findByKeyWord", method = RequestMethod.GET)
    public CommonResult findByKeyWord ( SysLog log ) {
        List<SysLog> byKeyWord = logService.findByKeyWord ( log );
        try {
            if (byKeyWord==null) {
                return CommonResult.failed ( "关键字查询日志失败!" );
            } else {
                return CommonResult.success ( byKeyWord,"关键字查询日志成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }


    @ApiOperation(value = "后台日志删除", notes = "后台日志删除")
    @RequestMapping(value = "/deleteLogById", method = RequestMethod.DELETE)
    public CommonResult deleteLogById ( @RequestParam(required = false) String id ) {
        Integer integer = logService.deleteLogById ( id );
        try {
            if (integer < 0) {
                return CommonResult.failed ( "删除" + integer + "条日志失败!" );
            } else {
                return CommonResult.success ( id,"删除" + integer + "条日志成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }


    @ApiOperation(value = "后台日志批量删除", notes = "后台日志批量删除")
    @RequestMapping(value = "/batchLogDelete", method = RequestMethod.DELETE)
    public CommonResult batchLogDelete ( @RequestParam String id ) {
        try {
            List list = new ArrayList ( );
            String[] strings = id.split ( "," );
            for (String string : strings) {
                list.add ( string );
            }
            Integer integer = logService.batchLogDelete ( list );
            if (integer == null) {
                return CommonResult.failed ( "日志批量删除失败" );
            } else {
                System.out.println ( "日志批量删除" + integer + "条数据成功！" );
                return CommonResult.success ( integer,"日志批量删除成功！" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

}
